﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;
using System.Linq;


namespace Com{
    /// <summary>
    /// Interaction logic for ucWaterCoefficients.xaml
    /// </summary>
    public partial class ucWaterCoefficients : UserControl
    {
        Boolean change = true;
        public ucWaterCoefficients()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tb_water_coefficients wc = new tb_water_coefficients();
                wc.Temperature = Convert.ToDouble(cbxDescription.Text);
                wc.Density01 = Convert.ToDouble(txtDensity.Text);
                wc.Density02 = Convert.ToDouble(txtDensity02.Text);
                wc.DynamicViscosity = Convert.ToDouble(txtDynamicViscosity.Text);
                wc.HeatConductivity = Convert.ToDouble(txtHeatConductivity.Text);
                wc.HeatSpecific = Convert.ToDouble(txtHeatSpecific.Text);
                wc.KinematicViscosity = Convert.ToDouble(txtKinematicViscosity.Text);
                wc.Prandtl_Coefficient = Convert.ToDouble(txtPrandtlCoefficient.Text);
                wc.IncreasedEvaporationCoefficient = Convert.ToDouble(txtIncreasedCoefficient.Text);
             
                    LibraryManagerInCom.addWaterCoefficient(wc);
                    enableButton();
                    loadData();
              
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            loadData();
            enableButton();
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxDescription.Text = "";
            txtDensity.Text = "";
            txtDynamicViscosity.Text = "";
            txtHeatConductivity.Text = "";
            txtHeatSpecific.Text = "";
            txtKinematicViscosity.Text = "";
            txtPrandtlCoefficient.Text = "";
            txtIncreasedCoefficient.Text = "";
            txtDensity02.Text= "";
            disableButton();
        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_water_coefficients wc = (tb_water_coefficients)cbxDescription.SelectedItem;
            tb_water_coefficients ac2 = LibraryManagerInCom.copyWaterCoefficient(wc);

            cbxDescription.Text = ac2.Temperature.ToString();
            txtDensity.Text = ac2.Density01.ToString();
            txtDensity02.Text = ac2.Density02.ToString();
            txtDynamicViscosity.Text = ac2.DynamicViscosity.ToString();
            txtHeatConductivity.Text = ac2.HeatConductivity.ToString();
            txtHeatSpecific.Text = ac2.HeatSpecific.ToString();
            txtPrandtlCoefficient.Text = ac2.Prandtl_Coefficient.ToString();
            txtKinematicViscosity.Text = ac2.KinematicViscosity.ToString();
            txtIncreasedCoefficient.Text = ac2.IncreasedEvaporationCoefficient.ToString();

            disableButton();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                try
                {
                    tb_water_coefficients wc = (tb_water_coefficients)cbxDescription.SelectedItem;
                    LibraryManagerInCom.delWaterCoefficient(wc);

                    // Cập nhật lại dữ liệu cho control
                    loadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion

        public void enableButton()
        {
            change = true;

            cbxDescription.IsEditable = false;

            btnCancel.IsEnabled = false;
            btnSave.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnDelete.IsEnabled = true;
            btnCopy.IsEnabled = true;
        }

        public void disableButton()
        {
            change = false;

            cbxDescription.IsEditable = true;
            btnCancel.IsEnabled = true;
            btnSave.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnDelete.IsEnabled = false;
            btnCopy.IsEnabled = false;
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            enableButton();
            loadData();
        }

        public void loadData()
        {
            cbxDescription.Items.Clear();
            //cbxDescription.ItemsSource = LibraryManagerInCom.LocationList;
            foreach (var item in LibraryManagerInCom.WaterCoefficientsList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "Temperature";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxDescription.SelectedItem != null && change)
            {
                tb_water_coefficients ac = (tb_water_coefficients)cbxDescription.SelectedItem;
                txtDensity.Text = ac.Density01.ToString();
                txtDynamicViscosity.Text = ac.DynamicViscosity.ToString();
                txtHeatConductivity.Text = ac.HeatConductivity.ToString();
                txtHeatSpecific.Text = ac.HeatConductivity.ToString();
                txtKinematicViscosity.Text = ac.KinematicViscosity.ToString();
                txtPrandtlCoefficient.Text = ac.Prandtl_Coefficient.ToString();
                txtIncreasedCoefficient.Text = ac.IncreasedEvaporationCoefficient.ToString();
                txtDensity02.Text = ac.Density02.ToString();

            }
        }

       
    }
}
